#=
世界线位置
=#

using AbstractAlgebra
using Random

const WrdLn{T} = Array{T, 2}
const WrdFb{T} = Vector{WrdLn{T}}

"""
产生一条世界线
"""
function WrdLn(::Type{T}, Nt::Int; Nd::Int=3) where {T}
    return zeros(T, Nt, Nd)
end


"""
产生多条世界线
"""
function WrdFb(::Type{T}, Np::Int, Nt::Int; Nd::Int=3) where {T}
    return [zeros(T, Nt, Nd) for _ in Base.OneTo(Np)]
end


"""
一个切片上的位置
"""
function beads(fb::WrdFb{T}, ti::Int) where T
    Np = length(fb)
    Nt, Nd = size(fb[1])
    ret = Matrix{T}(undef, Np, Nd)
    for ip in Base.OneTo(Np)
        ret[ip, :] = fb[ip][ti, :]
    end
    return ret
end


"""
进行一次置换
"""
function sign_permute!(fb::WrdFb)
    Np = length(fb)
    arr = collect(Base.OneTo(Np))
    shuffle!(arr)
    sgn = sign(Perm(arr))
    permute!(fb, arr)
    return sgn, fb
end


"""
周期边条件？？？
"""


"""
随机创建一个worldline的形状
"""
function random_path(::Type{T}, Np::Int, Nt::Int, vol::Vector{T}; λe=_ThermWL) where {T}
    wfb = WrdFb(T, Np, Nt)
    pos = rand(Np, 3)
    for id in Base.OneTo(3)
        pos[:, id] = pos[:, id] * vol[id]
    end
    off = (rand(Nt-1, Np, 3).-0.5)*λe
    for ip in Base.OneTo(Np)
        wfb[ip][1, :] = map_to_1st(pos[ip, :], vol)
        for it in 2:1:Nt
            wfb[ip][it, :] = map_to_1st(pos[ip, :] + off[it-1, ip, :], vol)
        end
    end
    return wfb
end


"""
随机创建一个worldline的形状
"""
function random_point(::Type{T}, Np::Int, Nt::Int, vol::Vector{Float64}) where {T}
    wfb = WrdFb(T, Np, Nt)
    pos = rand(Np, 3)
    for id in Base.OneTo(3)
        pos[:, id] = pos[:, id] * vol[id]
    end
    for ip in Base.OneTo(Np)
        for it in 1:1:Nt
            wfb[ip][it, :] = map_to_1st(pos[ip, :], vol)
        end
    end
    return wfb
end

